package com.example.testrxjava.activity.search;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.testrxjava.GZCode;
import com.example.testrxjava.MainActivity;
import com.example.testrxjava.R;
import com.example.testrxjava.base.BaseActivity;
import com.example.testrxjava.util.Action;
import com.example.testrxjava.util.AppManager;
import com.example.testrxjava.util.CommUtil;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class SearchActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.searchHotFlowLayout)
    TagFlowLayout lv;
    @BindView(R.id.backIv)
    ImageView backIv;

    @BindView(R.id.searchEdit)
    EditText searchEdit;
    @BindView(R.id.searchTv)
    TextView searchTv;

    List<HotKeyBean> data;
    List<HotKeyBean.DataBean> list=new ArrayList<HotKeyBean.DataBean>();

    private String searchinfo;
    @Override
    public int getLayoutId() {
        return R.layout.activity_search;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void init(Bundle savedInstanceState) {
        data=new ArrayList<HotKeyBean>();
            initData();
        initLinstener();
    }

    private void initData() {
        Action.getInstance().getHotKey(new SearchSucessCallback() {
            @Override
            public void searchSucess(HotKeyBean bean) {
                HotKeyBean.DataBean dataBean = null;
                for (int i = 0; i < bean.getData().size(); i++) {
                    Log.e("公众号---",bean.getData().get(i).getName());
//                  bean =gzCode.getData().get(i);
                }
//                data.add(gzCode);
                data.add(bean);
                initLayout();
            }

            @Override
            public void searchFail(String message) {
                CommUtil.Toast(SearchActivity.this,message);
            }
        });

    }

    private void initLayout() {

        for (int i = 0; i < data.size(); i++) {
            list=data.get(i).getData();
        }
        lv.setAdapter(new TagAdapter<HotKeyBean.DataBean>(list) {
            @Override
            public View getView(FlowLayout parent, int position, HotKeyBean.DataBean dataBean) {
                TextView tv = (TextView) LayoutInflater.from(SearchActivity.this).inflate(R.layout.flow_layout_tv,
                        parent, false);
                String name = null;
//                    for (int j = 0; j < dataBean.getData().size(); j++) {
                Log.e("===", position + "");
                Log.e("d--", dataBean.getName());
                name = dataBean.getName();
                tv.setText(name);
                tv.setBackgroundColor(CommUtil.randomTagColor());
                Log.e("name===", name);


//                }

                return tv;
            }
        });

        lv.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                Log.e("搜索热词",list.get(position).getName());
                toSearchInfo(list.get(position).getName());
                return true;
            }
        });

    }

    private void getSearchInfo(){
        searchinfo=searchEdit.getText().toString().trim();
    }

    private void toSearchInfo(String key) {
        if(TextUtils.isEmpty(key)){
            CommUtil.Toast(this,"不能为空！");
            return;
        }

        SearchInfoActivity.startActivityBySearchActivity( this,key);



    }

    @Override
    public void initLinstener() {
            backIv.setOnClickListener(this);
            searchTv.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.backIv:
                finish();
                break;
            case R.id.searchTv:
                getSearchInfo();
                if(TextUtils.isEmpty(searchinfo)){
                    CommUtil.Toast(this,"输入不能为空");
                    return;
                }

                SearchInfoActivity.startActivityBySearchActivity( this,searchinfo);



                break;
        }
    }
}
